// Plugin$MxName$.cpp : Defines the exported functions for the DLL application.
//


#include "stdafx.h"
#include "$projectname$.h"

void  *MXSTDMETHOD CreateClassInstance(const GUID IId)
{
	void *rc = nullptr;

	if ( MxGUID::IsEqual(IId, I$projectname$IID::Guid()))
		rc = new $projectname$();
	else if ( MxGUID::IsEqual(IId, IMxPluginLibDetailsIID::Guid()))
		rc = new $projectname$();
	else if ( MxGUID::IsEqual(IId, IMxPluginBaseIID::Guid()))
		rc = new $projectname$();
	else
		rc = nullptr;

	if (rc == nullptr)
	{
		std::string tmp;
		MX_SETERROR(MX1801, MxError::Install, MxError::Abort, MxError::QuietReport, "CreateClassInstance, unsupported IID=%s", MxGUID::ConvertToString(IId, &tmp));
	}
	else
		((IMxPluginBase *)rc)->DuplicatePtr();

	return rc;
}

$projectname$::$projectname$(void): 
	_refCnt(0)
{
}

$projectname$::~$projectname$(void)
{
}

long MXSTDMETHOD $projectname$::DestroyPtr(void)
{
	long cnt = InterlockedDecrement(&_refCnt);
	if ( cnt == 0 )
		delete this;
	return cnt;
}

long MXSTDMETHOD $projectname$::DuplicatePtr(void)
{
	return InterlockedIncrement(&_refCnt);
}

void *MXSTDMETHOD $projectname$::Dynamic_cast(const GUID IId)
{
	void *rc = nullptr;

	if ( MxGUID::IsEqual(IId, I$projectname$IID::Guid()))
		rc = static_cast<I$projectname$ *>(this);
	else if ( MxGUID::IsEqual(IId, IMxPluginLibDetailsIID::Guid()))
		rc = static_cast<IMxPluginLibDetails *>(this);
	else if ( MxGUID::IsEqual(IId, IMxPluginBaseIID::Guid()))
		rc = static_cast<IMxPluginBase *>(this);
	else
		rc = nullptr;
	std::string tmp;
	if (rc == nullptr)
		MX_SETERROR(MX1802, MxError::Install, MxError::Abort, MxError::QuietReport, "Dynamic_cast does not support IID=%s", MxGUID::ConvertToString(IId, &tmp));
	else
		((IMxPluginBase *)rc)->DuplicatePtr();

	return rc;
}

int			MXSTDMETHOD $projectname$::GetInterfaceCount()
{ 
	return 3; 
}

bool		MXSTDMETHOD $projectname$::IsInterfaceSupported(const GUID iid)
{ 
	bool rc = false;

	if( MxGUID::IsEqual(iid, I$projectname$IID::Guid()) )
		rc = true;
	else
	{
		if( MxGUID::IsEqual(iid, IMxPluginLibDetailsIID::Guid()) )
			rc = true;
		else
		{
			if( MxGUID::IsEqual(iid, IMxPluginBaseIID::Guid()) )
				rc = true;
		}
	}
	return rc;
}


const GUID	MXSTDMETHOD $projectname$::GetIID(int index) 
{ 
	GUID rc = GUID_Invalid;
	switch (index)
	{
		case 0:
			 rc = I$projectname$IID::Guid();
		break;
		case 1:
			rc = IMxPluginLibDetailsIID::Guid();
		break;
		case 2:
			rc = IMxPluginBaseIID::Guid();
		break;
		default:
		break;
	}
	return rc; 
}

const char *MXSTDMETHOD  $projectname$::GetISummary( const GUID  iid)
{
	const char *rc = "";

	if( MxGUID::IsEqual(iid, I$projectname$IID::Guid()) )
		rc = "I$projectname$ - TODO: describe purpose of Plugin";
	else
	{
		if( MxGUID::IsEqual(iid, IMxPluginLibDetailsIID::Guid()) )
			rc = "IMxPluginLibDetails - provides details about MxPlugin and its interfaces";
		else
		{
			if( MxGUID::IsEqual(iid, IMxPluginBaseIID::Guid()) )
				rc = "IMxPluginBase - base interface for Plugins (similar to COM IUnknown)";
		}
	}
	return rc;
}

const char *MXSTDMETHOD  $projectname$::GetIDetails(const GUID  iid)
{
	const char *rc = "";

	if( MxGUID::IsEqual(iid, I$projectname$IID::Guid()) )
		rc = "see I$projectname$.h for details about methods in this interface";
	else
	{
		if( MxGUID::IsEqual(iid, IMxPluginLibDetailsIID::Guid()) )
			rc = "see IMxPluginLibDetails.h for details about methods in this interface";
		else
		{
			if( MxGUID::IsEqual(iid, IMxPluginBaseIID::Guid()) )
				rc = "see IMxPluginBase.h for details about methods in this interface";
		}
	}
	return rc;
}
